//
//  NFDescribeView.m
//  PhotoBrowser
//
//  Created by A_Dirt on 16/5/25.
//  Copyright © 2016年 程印. All rights reserved.
//

#define SCREEN_HEIGHT                   [[UIScreen mainScreen] bounds].size.height
//width of window when equipment vertical screen
#define SCREEN_WIDTH                    [[UIScreen mainScreen] bounds].size.width

#define ScaleFloat(x) x * SCREEN_WIDTH / 320

#define ViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]

#import "NFDescribeView.h"
#import "NFDescribeViewCell.h"
#import "UIView+SDAutoLayout.h"

@interface NFDescribeView ()
{
    UITableView *_tableView;
    
    UIView   * commentsView;
    
    //界面数据
    NFNewsEntity * _entity;
    
    NSString * _page;

}
@property (assign) CGRect nosecPersonFrame;

@end

@implementation NFDescribeView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        self.clipsToBounds = YES;
        
        self.backgroundColor = [UIColor clearColor];
        
        self.nosecPersonFrame =frame;
        
        [self setUpTableView];
        
        [self setUpCommentsView];
    }

    
    return self;
}

- (void)setUpTableView
{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorColor = [UIColor clearColor];
    [self addSubview:_tableView];
    
    _tableView.sd_layout
    .leftSpaceToView(self,0)
    .rightSpaceToView(self,0)
    .topSpaceToView(self,0)
    .bottomSpaceToView(self,44);
    
    [_tableView registerClass:[NFDescribeViewCell class] forCellReuseIdentifier:@"NFDescribeViewCell"];
    

}

- (void)showViewWithinfo:(NSArray *)info index:(NSInteger )index
{
    NFNewsEntity * model = [info objectAtIndex:index];
    
    if (model == _entity) {
        return;
    }
    
    _entity = [info objectAtIndex:index];
    
    _page = [NSString stringWithFormat:@"%@/%@",@(index+1),@(info.count)];
    
    [_tableView setContentOffset:CGPointMake(0, 0) animated:NO];
    
    _tableView.bounces = self.bounds.size.height - 44 > [self commentsHeight:_entity.newsContent]?NO:YES;
    
    [_tableView reloadData];
    
    
}

//写跟帖
- (void)setUpCommentsView
{
    commentsView = [[UIView alloc] init];
    commentsView.backgroundColor = [UIColor blackColor];
    [self addSubview:commentsView];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [commentsView addSubview:lineView];
    
    UIView *textFiled = [[UIView alloc] init];
    ViewBorderRadius(textFiled, 15, 0.5, [UIColor lightGrayColor]);
    [commentsView addSubview:textFiled];
    
    UILabel * placeholderLab = [[UILabel alloc] init];
    placeholderLab.font = [UIFont systemFontOfSize:14];
    placeholderLab.textColor = [UIColor lightGrayColor];
    placeholderLab.textAlignment = NSTextAlignmentLeft;
    placeholderLab.text = [NSString stringWithFormat:@"写跟帖"];
    [textFiled addSubview:placeholderLab];
    
    
    
    UIButton * shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [shareBtn setTitle:@"分享" forState:UIControlStateNormal];
    shareBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    shareBtn.titleLabel.textColor = [UIColor whiteColor];
    [commentsView addSubview:shareBtn];
    
    commentsView.sd_layout
    .leftSpaceToView(self,0)
    .rightSpaceToView(self,0)
    .topSpaceToView(_tableView,0)
    .bottomSpaceToView(self,0);
    
    lineView.sd_layout
    .leftSpaceToView(commentsView,0)
    .rightSpaceToView(commentsView,0)
    .topSpaceToView(commentsView,0)
    .heightIs(1);
    
    shareBtn.sd_layout
    .rightSpaceToView(commentsView,15)
    .topSpaceToView(commentsView,0)
    .bottomSpaceToView(commentsView,0)
    .widthIs(50);
    
    textFiled.sd_layout
    .leftSpaceToView(commentsView,15)
    .rightSpaceToView(shareBtn,15)
    .topSpaceToView(commentsView,5)
    .bottomSpaceToView(commentsView,5);
    
    placeholderLab.sd_layout
    .leftSpaceToView(textFiled,10)
    .rightSpaceToView(textFiled,10)
    .topSpaceToView(textFiled,0)
    .bottomSpaceToView(textFiled,0);
    

}

//向上滑动时   
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat changeHeight = scrollView.contentOffset.y;

    //下拉
    if (changeHeight < 0.0f)
    {
        self.frame = self.nosecPersonFrame;
        
    }
    //向上滑动
    else if (changeHeight > 0.0f)
    {
        CGFloat hidePerHeight = changeHeight;

        if (hidePerHeight <= self.nosecPersonFrame.size.height)
        {
            self.frame = CGRectMake(self.nosecPersonFrame.origin.x,
                                    self.nosecPersonFrame.origin.y - hidePerHeight,
                                    self.nosecPersonFrame.size.width,
                                    self.nosecPersonFrame.size.height + hidePerHeight);
            
        }
        else if (hidePerHeight > (self.nosecPersonFrame.size.height-44))
        {

            //防止滑动过快，
            if (self.frame.origin.y > - (self.nosecPersonFrame.size.height-44))
            {

                self.frame = CGRectMake(self.nosecPersonFrame.origin.x,
                                        self.nosecPersonFrame.origin.y - hidePerHeight,
                                        self.nosecPersonFrame.size.width,
                                        self.nosecPersonFrame.size.height + hidePerHeight);
            }
        }
        
        
    }
    else if (changeHeight == 0.0f)
    {
        //视图还原
        self.frame = self.nosecPersonFrame;
    }
    

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{


}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NFDescribeViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"NFDescribeViewCell"];

    [cell showViewEntity:_entity page:_page];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return [self commentsHeight:_entity.newsContent] + 50;
}

- (CGFloat)commentsHeight: (NSString *)conten
{
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectZero];
    [lab setNumberOfLines:0];
    lab.textAlignment = NSTextAlignmentCenter;
    lab.font = [UIFont systemFontOfSize:14];
    lab.frame = CGRectMake(0, 0, SCREEN_WIDTH - 30, 0);
    lab.text = conten;
    [lab sizeToFit];
    return lab.bounds.size.height;
    
}

@end
